# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=installer-framework
pkgbase=mingw-w64-qt-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-qt-${_realname}"
pkgdesc="The Qt Installer Framework used for the Qt SDK installer (mingw-w64)"
pkgver=4.5.0
pkgrel=2
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://www.qt.io/developers/'
license=('GPL3' 'LGPL' 'FDL' 'custom')
conflicts=("${MINGW_PACKAGE_PREFIX}-qt-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-qt-${_realname}-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-qt5-static"
             "${MINGW_PACKAGE_PREFIX}-libwebp"
             "${MINGW_PACKAGE_PREFIX}-jasper"
             "${MINGW_PACKAGE_PREFIX}-bzip2")
source=(https://download.qt.io/official_releases/qt-${_realname}/${pkgver}/${_realname}-opensource-src-${pkgver}.tar.xz
        "0002-add-installation-stuff-to-pro-files.patch"
        "0003-add-recursive-option-to-rmdir-operation.patch"
        "0004-add-dirExists-fuction.patch"
        "0006-Don-t-remove-files-that-live-outside-of-path.patch"
        "0007-Don-t-prevent-qmake-linking-qmltooling-plugin.patch"
        "0010-fix-version-pass-to-windres.patch"
        "0011-fix-redefined-mode_t.patch"
        "0012-get_timezone-not-available-on-msvcrt.patch")
sha256sums=('153a93e7ab3b237eb4b4bded39ae41ef6b9a8148f1b8dadd811d49db06b207b6'
            '4100a93ac7e117a32430d22187a4ebe94e7830d94d2c23b85651bc1d95821409'
            '4db7a4c3e6ea3a26de6493e4e2b683251060c7c8cefacf6bfa458026e541e906'
            '2aaf51f346edb6db2367262d0d8e1bb77d0afee51fabd5b2766038a97ebf0ca6'
            '00857e9a654ecaed93ec79898389329799fdc01968b7562ae1889498b12c943d'
            'a82b4dbddebb42111832ab4dd79ec591d4a7196dea4b59f0dc77bb7615181b9e'
            'f7293902981c69e642c59276cee2a24c0d0540f8fddfc4ac9f9a6b22cf0151ed'
            'a2eaac4c11e0b6a41f3289657d9e95894a5ed6d625ea98cc41fd4356d2d22a8e'
            '51d50531fa3cc4f68473d51812f5feea128690dc8bc7de379bf9888dae9b9d46')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-opensource-src-${pkgver%.*}

  apply_patch_with_msg \
    0002-add-installation-stuff-to-pro-files.patch \
    0003-add-recursive-option-to-rmdir-operation.patch \
    0004-add-dirExists-fuction.patch \
    0007-Don-t-prevent-qmake-linking-qmltooling-plugin.patch \
    0010-fix-version-pass-to-windres.patch \
    0011-fix-redefined-mode_t.patch \
    0012-get_timezone-not-available-on-msvcrt.patch

  # This requires ../mingw-w64-qt5-static/0044-qt-5.4.1-Revert-Revert-fix-NTFS-mount-points.patch
  # to fix the problem for junction points. Together they fix:
  # https://github.com/msys2/msys2.github.io/issues/4
  apply_patch_with_msg \
    0006-Don-t-remove-files-that-live-outside-of-path.patch
}

build() {
  mkdir build-${MSYSTEM} && cd build-${MSYSTEM}

  local _config
  if check_option "debug" "n"; then
    _config="release"
  else
    _config="debug"
  fi

  ${MINGW_PREFIX}/qt5-static/bin/qmake.exe \
    CONFIG+="${_config}" \
    CONFIG+=libarchive \
    CONFIG+=no_testcase_installs \
    IFW_ZLIB_LIBRARY=${MINGW_PREFIX}/lib/libz.a \
    IFW_BZIP2_LIBRARY=${MINGW_PREFIX}/lib/libbz2.a \
    IFW_LZMA_LIBRARY=${MINGW_PREFIX}/lib/liblzma.a \
    ../${_realname}-opensource-src-${pkgver%.*}/installerfw.pro

  make
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  make INSTALL_ROOT="${pkgdir}${MINGW_PREFIX}" install
  rm -rf "${pkgdir}${MINGW_PREFIX}"/lib

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 "${srcdir}"/${_realname}-opensource-src-${pkgver%.*}/LICENSE* "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}
}
